var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: false,
    isdefault:1
  },

  switchChange:function(e){
    let isdefault = this.data.isdefault;
    if (e.detail.value){
      isdefault = 2
    }else{

      isdefault = 1
    }
    console.log(isdefault)
    this.setData({
      isdefault: isdefault
    })
  },

  formSubmit:function(e){
    let that=this;
    let id = e.detail.value.id;
    let last_name = e.detail.value.last_name;
    let phone = e.detail.value.phone.trim();
    let province = e.detail.value.province;
    let city = e.detail.value.city;
    let zip = e.detail.value.zip;
    let address = e.detail.value.address;
    let is_default = e.detail.value.is_default;
    var mobile = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    var isMobile = mobile.exec(phone);
    if (!isMobile) {
      wx.showModal({
        title: "提示",
        content: '你输入的电话不符，请重新检查填写'
      })
      return false;
    }
    app.getUserToken(function (err, token) {
      wx.showLoading({
        title: '加载中...',
      })
      wx.request({
        url: app.globalData.url +'/index.php?_m=shop/customers_address/save&v=v20190909b',
        data: { id: id, last_name: last_name, phone: phone, province: province, city: city, zip: zip, address: address, is_default: is_default, auth_token: token },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        success: (res) => {
          wx.hideLoading();
          if (res.data.status == 401) {
            // 清空授权token
            app.globalData.token = null
            wx.removeStorageSync('token')
            that.formSubmit();
          } else if (res.data.code == 0) {
            
              wx.redirectTo({
                url:'../address/address'
              })
          
          }
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  
  onShareAppMessage: function () {
    app.globalData.uid = app.globalData.uid || wx.getStorageSync('uid') || 0;
    return {
      title: '深圳智汇创想科技有限责任公司员工福利购!',
      path: '/pages/index/index?fid=' + app.globalData.uid
    }
  }
})